package com.example.day03

import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.day03.databinding.ActivityLoginBinding
import com.example.day03.login.LoginViewModel
import com.example.day03.mvvm.BaseActivity

class LoginActivity : BaseActivity<ActivityLoginBinding,LoginViewModel>() {
    override fun bindLayoutId(): Int {
        return R.layout.activity_login
    }

    override fun getViewModel(): Class<LoginViewModel> {
        return LoginViewModel::class.java
    }

    override fun initView() {
        val phone = intent.getStringExtra("phone")
        val pwd = intent.getStringExtra("pwd")
        binding.phone.setText(phone)
        binding.pass.setText(pwd)
        binding.dl.isEnabled=false
        binding.phone.addTextChangedListener(object:TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if (RegexUtils.isMobileExact(binding.phone.text.toString())){
                    binding.dl.isEnabled=true
                }else{
                    binding.dl.isEnabled=false
                }
            }
            override fun afterTextChanged(s: Editable?) {

            }
        })
        binding.dl.setOnClickListener {
            viewModel.getLogin(binding.phone.text.toString(),binding.pass.text.toString())
        }
    }

    override fun initDate() {
        viewModel.loginsuccess.observe(this){
            if (it.code==200){
                startActivity(Intent(this@LoginActivity,MainActivity::class.java))
            }
            ToastUtils.showLong(it.message)
        }
    }

}